﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DoAn.DAO;

namespace DoAn.Admin
{
    public partial class QuanLy : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                loadTaiKhoan();
                loadLoaiTaiKhoan();
            }
        }
        void loadTaiKhoan()
        {
            string sql = "select * from NGUOIDUNG as nd left outer join LOAINGUOIDUNG as lnd on nd.MALND = lnd.MALND";
            DataTable dt = SqlDataProvider.LoadData(sql);
            gvTaiKhoan.DataSource = dt;
            gvTaiKhoan.DataBind();
        }
        void loadLoaiTaiKhoan()
        {
            string sql = "select * from LOAINGUOIDUNG ";
            DataTable dt = SqlDataProvider.LoadData(sql);
            ddlLoaiTaiKhoan.DataSource = dt;
            ddlLoaiTaiKhoan.DataValueField = "MALND";
            ddlLoaiTaiKhoan.DataTextField = "TENLND";
            ddlLoaiTaiKhoan.DataBind();
        }
        protected void gvTaiKhoan_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (gvTaiKhoan.SelectedIndex > -1)
            {
                UpdatePanel1.Visible = true;
                btnAn.Visible = true;
                btnChiTiet.Visible = false;
                btnCapNhat.Visible = true;
                btnXoa.Visible = true;
                lbBiXoa.Visible = true;
                cbBiXoa.Visible = true;
                int idx = gvTaiKhoan.SelectedIndex;          
                lbMAND.Text = HttpUtility.HtmlDecode(gvTaiKhoan.Rows[idx].Cells[0].Text);
                txtTen.Text = HttpUtility.HtmlDecode(gvTaiKhoan.Rows[idx].Cells[1].Text);
                txtMatKhau.Text = HttpUtility.HtmlDecode(gvTaiKhoan.Rows[idx].Cells[2].Text);
                txtEmail.Text = HttpUtility.HtmlDecode(gvTaiKhoan.Rows[idx].Cells[3].Text);
                //GridViewRow row = gvTaiKhoan.SelectedRow;
                //Response.Write(row.Cells[3].Text);
                //ddlLoaiTaiKhoan.SelectedValue = row.Cells[3].Text;
                ddlLoaiTaiKhoan.SelectedValue = HttpUtility.HtmlDecode(gvTaiKhoan.Rows[idx].Cells[4].Text);

                txtNgayTao.Text = HttpUtility.HtmlDecode(gvTaiKhoan.Rows[idx].Cells[6].Text);
                CheckBox Chk1 = (CheckBox)gvTaiKhoan.Rows[idx].FindControl("chkKichHoat");
                if (Chk1.Checked == true)
                {
                    cbKichHoat.Checked = true;
                }
                else cbKichHoat.Checked = false;
                CheckBox Chk2 = (CheckBox)gvTaiKhoan.Rows[idx].FindControl("chkBiXoa");
                if (Chk2.Checked == true)
                {
                    cbBiXoa.Checked = true;
                }
                else cbBiXoa.Checked = false;

                //TextBox2.Text = HttpUtility.HtmlDecode(gvTaiKhoan.Rows[idx].Cells[3].ToString());

                btnThem.Visible = false;
                btnXoa.Visible = true;
                btnCapNhat.Visible = true;




                //TextBox2.Text = gvTaiKhoan.Rows[idx].FindControl("cbKichHoat").ToString();

                //CheckBoxField a = (CheckBoxField)(gvTaiKhoan.Rows[idx].Cells[6].Controls.ToString());

            }
        }

        protected void gvTaiKhoan_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int idx = e.RowIndex;
            string MAND = gvTaiKhoan.DataKeys[idx].Value.ToString();

            string sqlC = "select count(*) from NGUOIDUNG where MAND = '"+MAND+"'";
            int n = (int)SqlDataProvider.ExecSqlScalar(sqlC);

            if (n == 0)
            {
                string sql = "delete from NGUOIDUNG where MAND = '"+MAND+"'";
                SqlDataProvider.ExecSql(sql);
                loadTaiKhoan();
            }
            else Response.Write("KHÔNG XOÁ ĐƯỢC");
        }

        protected void btnThem_Click(object sender, EventArgs e)
        {
            XLMADAO x = new XLMADAO();
            string TenDN = txtTen.Text;
            string MatKhau = txtMatKhau.Text;
            string Email = txtEmail.Text;
            string Loai = ddlLoaiTaiKhoan.SelectedValue;
            string Ngay = txtNgayTao.Text;
            bool KichHoat = cbKichHoat.Checked;
            bool BiXoa = cbBiXoa.Checked;
            string year = x.Get_Year();
            string month = x.Get_Month();
            string day = x.Get_Day();

            string madau = Loai + year + month;
            string sql1 = "select * from NGUOIDUNG where MAND like '" + madau + "%'";
            DataTable dt = SqlDataProvider.LoadData(sql1);
            string stt = String.Format("{0:0000}", dt.Rows.Count + 1);
            string ma = madau + stt;
            string sql2 = "insert into NGUOIDUNG(MAND,TENDANGNHAP,MATKHAU,EMAIL,MALND,NGAYTAO,KICHHOAT,BIXOA) VALUES('" + ma + "','" + TenDN + "','" + MatKhau + "','" + Email + "','" + Loai + "','" + x.Get_ToDay() + "','" + KichHoat.ToString() + "','" + BiXoa.ToString() + "')";
            //TextBox2.Text = sql2;
            SqlDataProvider.ExecSql(sql2);
            loadTaiKhoan();
        }

        protected void gvTaiKhoan_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvTaiKhoan.EditIndex = e.NewEditIndex;
            loadTaiKhoan();
        }

        protected void btnXoa_Click(object sender, EventArgs e)
        {
            if (gvTaiKhoan.SelectedIndex > -1)
            {                
                int idx = gvTaiKhoan.SelectedIndex;
                string MAND = lbMAND.Text;
                string sql = "delete from NGUOIDUNG where MAND = '" + MAND + "'";
                //TextBox2.Text = sql;
                SqlDataProvider.ExecSql(sql);

                btnThem.Visible = true;
                btnXoa.Visible = false;
                btnCapNhat.Visible = false;

                gvTaiKhoan.SelectedIndex = -1;
                txtTen.Text = "";
                txtMatKhau.Text = "";
                txtEmail.Text = "";
                txtNgayTao.Text = "";
                cbKichHoat.Checked = false;
                cbBiXoa.Checked = false;
                loadTaiKhoan();
                btnThem.Visible = true;
            }
        }

        protected void btnCapNhat_Click(object sender, EventArgs e)
        {
            XLMADAO x = new XLMADAO();
            string TenDN = txtTen.Text;
            string MatKhau = txtMatKhau.Text;
            string Email = txtEmail.Text;
            string Loai = ddlLoaiTaiKhoan.SelectedValue;
            string Ngay = txtNgayTao.Text;
            bool KichHoat = cbKichHoat.Checked;
            bool BiXoa = cbBiXoa.Checked;
            if (gvTaiKhoan.SelectedIndex > -1)
            {
                string sql = "update NGUOIDUNG set TENDANGNHAP= '" + TenDN + "', MATKHAU='" + MatKhau + "',EMAIL='" + Email + "', MALND='" + Loai + "',NGAYTAO='" + Ngay + "',KICHHOAT='" + KichHoat.ToString() + "',BIXOA='" + BiXoa.ToString() + "' where MAND='" + lbMAND.Text + "'";
                //TextBox2.Text = sql;
                SqlDataProvider.ExecSql(sql);
                loadTaiKhoan();
                btnThem.Visible = true;
            }

        }

        protected void gvTaiKhoan_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            int idx = gvTaiKhoan.SelectedIndex;
            string MAND = gvTaiKhoan.DataKeys[idx].Value.ToString();

            string sqlC = "select count(*) from NGUOIDUNG where MAND = '" + MAND + "'";
            int n = (int)SqlDataProvider.ExecSqlScalar(sqlC);

            if (n == 0)
            {
                string sql = "delete from NGUOIDUNG where MAND = '" + MAND + "'";
                SqlDataProvider.ExecSql(sql);
                loadTaiKhoan();
            }
            else Response.Write("KHÔNG XOÁ ĐƯỢC");
        }

        protected void cbMatKhau_CheckedChanged(object sender, EventArgs e)
        {
            if (cbMatKhau.Checked == true)
                txtMatKhau.ReadOnly = false;
            else txtMatKhau.ReadOnly = true;
        }

        protected void btnChiTiet_Click(object sender, EventArgs e)
        {
            UpdatePanel1.Visible = true;
            btnThem.Visible = true;
            btnReset.Visible = true;
            btnChiTiet.Visible = false;
            btnAn.Visible = true;
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtTen.Text = "";
            txtMatKhau.Text = "";
            txtEmail.Text = "";
            txtNgayTao.Text = "";
            cbKichHoat.Checked = false;
        }

        protected void btnAn_Click(object sender, EventArgs e)
        {
            UpdatePanel1.Visible = false;
            btnChiTiet.Visible = true;
            btnAn.Visible = false;
        }

    }
}